package com.youlai.boot.system.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.system.model.form.LinkForm;
import com.youlai.boot.system.model.query.LinkPageQuery;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.system.model.vo.LinkPageVO;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * link控制器
 * @author haoxr
 * @since 2020/11/6
 */
@Tag(name = "09.link接口")
@RestController
@RequestMapping("/api/v1/link")
@RequiredArgsConstructor
public class LinkController {
    private final com.youlai.boot.system.service.LinkService LinkService;
    @Operation(summary = "Link分页列表")
    @GetMapping("/page")
    @Log(value = "文章分页列表", module = LogModuleEnum.USER)
    public PageResult<LinkPageVO> listPagedLinks(
            LinkPageQuery queryParams
    ) {
        IPage<LinkPageVO> result = LinkService.listPagedLinks(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增link")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:link:add')")
    @RepeatSubmit
    public Result<?> saveLink(
            @RequestBody @Valid LinkForm linkForm
    ) {
        boolean result = LinkService.saveLink(linkForm);
        return Result.judge(result);
    }

    @Operation(summary = "获取link表单数据")
    @GetMapping("/{linkId}/form")
    public Result<LinkForm> getArticleForm(
            @Parameter(description ="文章ID") @PathVariable Long linkId
    ) {
        LinkForm LinkForm = LinkService.getLinkForm(linkId);
        return Result.success(LinkForm);
    }

    @Operation(summary = "修改link")
    @PutMapping(value = "/{linkId}")
    @PreAuthorize("@ss.hasPerm('sys:link:edit')")
    public Result<?> updateLink(
            @Parameter(description = "linkId") @PathVariable Long linkId,
            @RequestBody @Validated LinkForm linkForm) {
        boolean result = LinkService.updateLink(linkId, linkForm);
        return Result.judge(result);
    }


    @Operation(summary = "删除link")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteLinks(
            @Parameter(description = "linkID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = LinkService.deleteLinks(ids);
        return Result.judge(result);
    }

}